#include <QMdiArea>
#include <QMenuBar>
#include <QToolBar>
#include <QApplication>
#include <QDockWidget>
#include <QStatusBar>

#include <filemanager.h>
#include <webinspector.h>
#include <parameterswindow.h>

#include "workbench.h"
#include "dataviewwindow.h"
#include "imageviewwindow.h"
#include "../constants.h"
#include "downloadcenter.h"
#include "../database/mysqldbmanager.h"

namespace Jcr
{

class WorkbenchPrivate
{
public:
    Jcr::FileManager* fileManager;
    Jcr::ParametersWindow* paramsWindow;
    Jcr::DownloadCenter* downloadCenter;
};

Workbench::Workbench(QWidget *parent) : QMainWindow(parent)
{
    d_ptr = new WorkbenchPrivate;
    init();
}

Workbench::~Workbench()
{
    delete d_ptr;
}

void Workbench::init()
{
    statusBar()->show();
    statusBar()->showMessage(tr("Ready"));

    setWindowTitle(tr("JCR Workbench"));

    d_ptr->fileManager = new Jcr::FileManager;
    connect(d_ptr->fileManager, SIGNAL(notify(QString)), this, SLOT(notify(QString)));
    setCentralWidget(d_ptr->fileManager);

    d_ptr->downloadCenter = new Jcr::DownloadCenter(this);
    connect(d_ptr->fileManager, SIGNAL(download(QUrl,QString,QString)), d_ptr->downloadCenter, SLOT(download(QUrl,QString,QString)));
    addDock(d_ptr->downloadCenter);

    d_ptr->paramsWindow = new Jcr::ParametersWindow(this);
    addDock(d_ptr->paramsWindow);

    createMenus();
    createToolBars();

    connect(d_ptr->fileManager, SIGNAL(modelChanged(QString)), d_ptr->paramsWindow, SLOT(setModel(QString)));
    connect(d_ptr->paramsWindow, SIGNAL(notify(QString)), this, SLOT(notify(QString)));

    Jcr::MySqlDbManager::instance();
}

void Workbench::createMenus()
{
    QMenu* fileMenu = menuBar()->addMenu(tr("&File"));

    QAction* exitAct = createAction(Jcr::Action::FILE_EXIT, SLOT(onExit()));
    fileMenu->addAction(exitAct);

    QMenu* editMenu = menuBar()->addMenu(tr("&Edit"));
    QAction* copyAct = createAction(Jcr::Action::EDIT_COPY, SLOT(onCopy()));
    QAction* cutAct = createAction(Jcr::Action::EDIT_CUT, SLOT(onCut()));
    QAction* pasteAct = createAction(Jcr::Action::EDIT_PASTE, SLOT(onPaste()));
    editMenu->addAction(copyAct);
    editMenu->addAction(cutAct);
    editMenu->addAction(pasteAct);

    QMenu* helpMenu = menuBar()->addMenu(tr("&Help"));
}

void Workbench::createToolBars()
{
    m_fileBar = new QToolBar(tr("File"));
    m_fileBar->addAction(createAction(Jcr::Action::FILE_EXIT));
    addToolBar(m_fileBar);
}

QAction* Workbench::createAction(const QString& text, const char* slot, const QIcon& icon)
{
    if (m_actionMap.contains(text))
        return m_actionMap.value(text);

    QAction* act = new QAction(text, this);
    act->setIcon(icon);
    connect(act, SIGNAL(triggered()), this, slot);
    m_actionMap.insert(text, act);
    return act;
}

void Workbench::onExit()
{
    qApp->exit(0);
}

void Workbench::onCopy()
{

}

void Workbench::onCut()
{

}

void Workbench::onPaste()
{

}

void Workbench::notify(const QString &msg)
{
    statusBar()->showMessage(msg);
}

void Workbench::addDock(QWidget* widget, Qt::DockWidgetArea area)
{
    QDockWidget* dw = new QDockWidget(widget->windowTitle());
    dw->setWidget(widget);
    dw->setWindowTitle(widget->windowTitle());
    addDockWidget(area, dw);
}


} // namespace Jcr
